﻿using BCCommon;
using BCCommon.TMS.FreightEnums;
using BCCommon.TMS.Order;
using BCCommon.TMS.Transport;
using BCDto.MySql.FreightConfigurationExtention;
using BCService.Common.Area;
using BCService.TMS.FreightConfigurationExtention;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;

namespace BCTMSRESTful.Controllers.Management.TMS.FreightConfiguration
{
    /// <summary>
    /// 零担运费配置
    /// </summary>
    [Route("api/v1/sim/[controller]")]
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    public class FreightConfigurationExtentionController : ManagementBaseController
    {
        private readonly ICountyService countyService;
        private readonly IProvinceService provinceService;
        private readonly ICityService cityService;
        private readonly IFreightConfigurationExtentionService freightConfigurationExtentionService;

        public FreightConfigurationExtentionController(
            ICountyService countyService,
            IProvinceService provinceService,
            ICityService cityService,
            IFreightConfigurationExtentionService freightConfigurationExtentionService)
        {
            this.countyService = countyService;
            this.provinceService = provinceService;
            this.cityService = cityService;
            this.freightConfigurationExtentionService = freightConfigurationExtentionService;
        }

        #region 公共方法

        /// <summary>
        /// 新增零担运费配置
        /// </summary>
        /// <param name="dto"></param>
        /// <response code="200">创建车辆成功。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("新增零担运费配置")]
        [ProducesResponseType(typeof(FreightConfigurationExtentionDto), 200)]
        public IActionResult Post([FromBody] FreightConfigurationExtentionRequestDto dto)
        {
            //1 检查省是否存在
            CheckProvince(dto.ProvinceCode);
            //2 检查市是否存在
            CheckCommounity(dto.CityCode);
            //3 检查区/县是否存在
            CheckCounty(dto.CountyCode);
            //4 创建
            dto.Editer = Admin.FullName;
            var result = freightConfigurationExtentionService.InsertAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="freightConfigurationExtentionId">运费配置id</param>
        /// <param name="dto"></param>
        /// <response code="201">数据修改成功。</response>
        /// <returns></returns>
        [HttpPut("{freight_configuration_extention_id}")]
        [Log("修改零担运费配置")]
        [ProducesResponseType(typeof(FreightConfigurationExtentionDto), 201)]
        public IActionResult Put([FromRoute(Name = ("freight_configuration_extention_id"))] int freightConfigurationExtentionId, [FromBody] FreightConfigurationExtentionPutDto dto)
        {
            ////1 检查省是否存在
            //CheckProvince(dto.ProvinceCode);
            ////2 检查市是否存在
            //CheckCommounity(dto.CityCode);
            ////3 检查区/县是否存在
            //CheckCounty(dto.CountyCode);
            //4 修改
            dto.Editer = Admin.FullName;
            var result = freightConfigurationExtentionService.UpdateAsync(Admin.IsSystemAccount, freightConfigurationExtentionId, dto);
            return this.Ok(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="freightConfigurationExtentionId">运费配置id</param>
        /// <response code="204">数据删除成功。</response>
        /// <returns></returns>
        [HttpDelete("{freight_configuration_extention_id}")]
        [Log("删除零担运费配置")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = ("freight_configuration_extention_id"))]int freightConfigurationExtentionId)
        {
            if (!Admin.IsSystemAccount)
                throw new ArgumentException("抱歉，您没有权限删除");

            freightConfigurationExtentionService.DeleteAsync(freightConfigurationExtentionId);
            return NoContent();
        }

        /// <summary>
        /// 通过id查询
        /// </summary>
        /// <param name="freightConfigurationExtentionId">运费配置id</param>
        /// <returns></returns>
        [HttpGet("[action]/{freight_configuration_extention_id}")]
        [ProducesResponseType(typeof(FreightConfigurationExtentionDto), 200)]
        public IActionResult GetById([FromRoute(Name = ("freight_configuration_extention_id"))]int freightConfigurationExtentionId)
        {
            var result = freightConfigurationExtentionService.GetById(freightConfigurationExtentionId);
            return Ok(result);
        }

        /// <summary>
        /// 分页列表查询
        /// </summary>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <param name="provinceCode">省编码</param>
        /// <param name="cityCode">市编码</param>
        /// <param name="countyCode">区/县编码</param>
        /// <param name="sourceType">订单来源</param>
        /// <param name="freightType">物流类型</param>
        /// <param name="vehicleTypeId">车型ID</param>
        /// <response code="200">数据查询成功。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<FreightConfigurationExtentionDto>), 200)]
        public IActionResult Query([FromQuery(Name = "province_code")]string provinceCode,
            [FromQuery(Name = "community_code")]string cityCode,
            [FromQuery(Name = "county_code")]string countyCode,
            [FromQuery(Name = "source_type")] OrderSourceType? sourceType,
            [FromQuery(Name = "freight_type")] FreightType? freightType,
            [FromQuery(Name = "vehicle_type_id")]int? vehicleTypeId,
            [FromQuery(Name = "page_index")]int pageIndex = 1,
            [FromQuery(Name = "page_size")]int pageSize = 10)
        {
            var result = freightConfigurationExtentionService.GetAll(this.Admin, new FreightConfigExtentionListRequestDto
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                ProvinceCode = provinceCode,
                CityCode = cityCode,
                CountyCode = countyCode,
                SourceType = sourceType,
                FreightType = freightType,
                VehicleTypeId = vehicleTypeId
            });
            return Ok(result);
        }

        /// <summary>
        /// 初始化修复不存在运费配置的区县
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult InitRepair()
        {
            var result = this.freightConfigurationExtentionService.InitRepair();
            return this.Ok(result);
        }

        #endregion 公共方法

        #region 私有方法

        /// <summary>
        /// 检查区/县是否存在
        /// </summary>
        /// <param name="countyCode"></param>
        private void CheckCounty(string countyCode)
        {
            var countyInfo = countyService.GetCountyByCode(countyCode);
            if (countyInfo == null)
                throw new ArgumentException("区/县不存在");
        }

        /// <summary>
        /// 检查市是否存在
        /// </summary>
        /// <param name="cityCode"></param>
        private void CheckCommounity(string cityCode)
        {
            var commountyInfo = cityService.Get(cityCode);
            if (commountyInfo == null)
                throw new ArgumentException("市不存在");
        }

        /// <summary>
        /// 检查省是否存在
        /// </summary>
        /// <param name="provinceCode"></param>
        private void CheckProvince(string provinceCode)
        {
            var provinceInfo = provinceService.GetProvinceEntity(provinceCode);
            if (provinceInfo == null)
                throw new ArgumentException("省不存在");
        }

        #endregion 私有方法
    }
}